package com.admission.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.xml.sax.SAXException;

import com.admission.model.Attachment;
import com.admission.model.EnrollDetail;
import com.admission.model.Notice;
import com.admission.model.Special;
import com.admission.service.AttachmentManager;
import com.admission.service.EnrollDetailManager;
import com.admission.service.NoticeManager;
import com.admission.service.SpecialManager;

/**
 * 用于生成静态页面方法 录取统计
 * 
 */
@Repository
@Transactional
public class StaticIndex {
	private final String PAGE = "index.html";
	// specialPage1
	private final String SPECIALPAGE1 = "<li class='normal'>###name###</li>";
	// specialPage2
	private final String SPECIALPAGE2 = "<li class='current1'><a href='base/special-article.action?spec_id=###id###'><img src='upload/###photoUrl###' title='###name###'/></a></li>";
	private final String NOTICENEW = "<li><img src='images/home_point1.png' class='list-detail-point' /><a href='base/notice!input.action?id=###id###' title='###title###' class='addNew'>###shorttitle###</a><span class='list-time'>###inputTime###</span></li>";
	private final String NOTICEPAGE = "<li><img src='images/home_point2.png'  class='list-detail-point'/><a href='base/notice!input.action?id=###id###' title='###title###' class='addNew'>###shorttitle###</a><span class='list-time'>###inputTime###</span></li>";
	private final String ENROLLDETAIL = "<li><img src='images/home_point3.png'  class='list-detail-point'/><a href='base/enroll-detail.action?year=###year###&&province=###provincenum###&&type=###typenum###' class='addNew'>###year### ###provincename### ###typename###招生计划</a><span class='list-time'>###inputTime###</span></li>";
	private final String NOTICEREAD = "<div class='listpro'><a href='base/notice!input.action?id=###id###' title='###title###'>###title###</a></div>";
	private final String ATTACHMENTPAGE = "<li><a href='upload/###url###' title='###name###'>###name###</a></li>";

	private ArrayUtils arrayUtils;
	private EnrollDetailManager enrollDetailManager;
	private AttachmentManager attachmentManager;
	private NoticeManager noticeManager;
	private SpecialManager specialManager;

	public void createHtmlForMainPage() throws IOException,
			IllegalArgumentException, SAXException, IllegalAccessException {
		Page<EnrollDetail> enrollDetail = new Page<EnrollDetail>(8);
		Page<Attachment> attachmentPage = new Page<Attachment>(4);
		Page<Special> specialPage = new Page<Special>(3);
		Page<Notice> noticePage = new Page<Notice>(PageUtils.MAIN_ENROLL);
		Page<Notice> noticeNew = new Page<Notice>(PageUtils.MAIN_ENROLL);
		Page<Notice> noticeRead = new Page<Notice>(PageUtils.MAIN_ENROLL);

		enrollDetailManager.getTitleList(enrollDetail);
		// 录取统计

		// recruitmentDetail = recruitmentDetailManager.getTitleList();
		// 资料下载
		attachmentPage.setOrder(Page.DESC);
		attachmentPage.setOrderBy("inputTime");
		attachmentManager.getPage(attachmentPage);
		// 专题页面
		List<PropertyFilter> filtersSpecial = new ArrayList<PropertyFilter>();
		PropertyFilter pSpecial = new PropertyFilter("EQS_home",
				Special.HOMETRUE);
		filtersSpecial.add(pSpecial);
		PropertyFilter pSpecial1 = new PropertyFilter("EQS_consult",
				Special.CONSULTTRUE);
		filtersSpecial.add(pSpecial1);
		specialPage.setOrder(Page.DESC);
		specialPage.setOrderBy("inputTime");
		specialManager.getPage(specialPage, filtersSpecial);
		// 最新导读
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		PropertyFilter p = new PropertyFilter("EQS_latestReading",
				Notice.READINGTRUE);
		filters.add(p);
		noticeNew.setOrder(Page.DESC);
		noticeNew.setOrderBy("inputTime");
		noticeManager.searchNotice(noticeNew, filters);
		// 招办公告
		noticePage.setOrder(Page.DESC);
		noticePage.setOrderBy("inputTime");
		noticeManager.getPage(noticePage);
		// 最新公告
		List<PropertyFilter> filters2 = new ArrayList<PropertyFilter>();
		PropertyFilter p2 = new PropertyFilter("EQS_latestAnnouncement",
				Notice.ANNOUNCEMENTTRUE);
		filters2.add(p2);
		noticeRead.setOrder(Page.DESC);
		noticeRead.setOrderBy("inputTime");
		noticeManager.searchNotice(noticeRead, filters2);

		Map<String, String> provincesmap = arrayUtils.getProvinceMap();
		Map<String, String> typemap = arrayUtils.getType();

		// 读取文档的时候，第一个字符无法读出，所以每行的第一个位置留空
		BufferedReader br = null;// 从字符输入流中读取文本，缓冲各个字符，从而实现字符、数组和行的高效读取

		br = new BufferedReader(new InputStreamReader(new FileInputStream(
				new File(FileUtils.getProjectRoot() + "/pages/masterplate/"
						+ PAGE)), "UTF-8"));// 从文件系统中的某个文件中获得输入字节

		String strs = null;
		StringBuffer sbf = new StringBuffer();
		while (br.read() != -1) {// 读取单个字符。
			sbf.append(br.readLine());// 读取一个文本行
			sbf.append("\n");// 换行
		}

		strs = sbf.toString();
		String specialPage1 = "";
		String specialPage2 = "";
		for (int i = 0; i < specialPage.getResult().size(); i++) {
			specialPage1 += SPECIALPAGE1.replaceAll("###name###", specialPage
					.getResult().get(i).getName());
			specialPage2 += SPECIALPAGE2
					.replaceAll("###photoUrl###",
							specialPage.getResult().get(i).getPhotoUrl())
					.replaceAll("###id###",
							specialPage.getResult().get(i).getId())
					.replaceAll("###name###",
							specialPage.getResult().get(i).getName());
		}
		String noticenew = "";
		for (int i = 0; i < noticeNew.getResult().size(); i++) {
			noticenew += NOTICENEW
					.replaceAll("###title###",
							noticeNew.getResult().get(i).getTitle())
					.replaceAll("###id###",
							noticeNew.getResult().get(i).getId())
					.replaceAll("###inputTime###",
							noticeNew.getResult().get(i).getInputTime())
					.replaceAll(
							"###shorttitle###",
							noticeNew.getResult().get(i).getTitle().length() > 35 ? noticeNew
									.getResult().get(i).getTitle()
									.substring(0, 35)
									+ "..."
									: noticeNew.getResult().get(i).getTitle());
		}
		String noticepage = "";
		for (int i = 0; i < noticePage.getResult().size(); i++) {
			noticepage += NOTICEPAGE
					.replaceAll("###title###",
							noticePage.getResult().get(i).getTitle())
					.replaceAll("###id###",
							noticePage.getResult().get(i).getId())
					.replaceAll("###inputTime###",
							noticePage.getResult().get(i).getInputTime())
					.replaceAll(
							"###shorttitle###",
							noticePage.getResult().get(i).getTitle().length() > 35 ? noticePage
									.getResult().get(i).getTitle()
									.substring(0, 35)
									+ "..."
									: noticePage.getResult().get(i).getTitle());
		}
		String enrolldetail = "";
		for (int i = 0; i < enrollDetail.getResult().size(); i++) {
			enrolldetail += ENROLLDETAIL
					.replaceAll("###year###",
							enrollDetail.getResult().get(i).getYear())
					.replaceAll(
							"###provincename###",
							provincesmap.get(enrollDetail.getResult().get(i)
									.getProvince()))
					.replaceAll("###provincenum###",
							enrollDetail.getResult().get(i).getProvince())
					.replaceAll("###typenum###",
							enrollDetail.getResult().get(i).getType())
					.replaceAll(
							"###typename###",
							typemap.get(enrollDetail.getResult().get(i)
									.getType()))
					.replaceAll("###inputTime###",
							enrollDetail.getResult().get(i).getInputTime());
		}

		String noticeread = "";
		for (int i = 0; i < noticeRead.getResult().size(); i++) {

			noticeread += NOTICEREAD.replaceAll("###id###",
					noticeRead.getResult().get(i).getId()).replaceAll(
					"###title###", noticeRead.getResult().get(i).getTitle());

		}
		String attachmentpage = "";
		for (int i = 0; i < attachmentPage.getResult().size(); i++) {

			attachmentpage += ATTACHMENTPAGE.replaceAll("###url###",
					attachmentPage.getResult().get(i).getUrl()).replaceAll(
					"###name###", attachmentPage.getResult().get(i).getName());

		}

		strs = strs.replaceAll("###specialPage1###", specialPage1)
				.replaceAll("###specialPage2###", specialPage2)
				.replaceAll("###noticepage###", noticepage)
				.replaceAll("###attachmentpage###", attachmentpage)
				.replaceAll("###noticeread###", noticeread)
				.replaceAll("###enrolldetail###", enrolldetail)
				.replaceAll("###noticenew###", noticenew);
		File file1 = new File(FileUtils.getProjectRoot() + "/index.html");
		if (!file1.exists()) {
			file1.getParentFile().mkdirs();
			file1.createNewFile();
		}
		FileOutputStream fos1 = new FileOutputStream(file1);
		OutputStreamWriter osw1 = new OutputStreamWriter(fos1, "UTF-8");
		PrintWriter outFile1 = new PrintWriter(osw1);
		outFile1.write(strs);// 写入
		osw1.close();
		fos1.close(); // 关闭资源
	}

	@Autowired
	public void setArrayUtils(ArrayUtils arrayUtils) {
		this.arrayUtils = arrayUtils;
	}

	@Autowired
	public void setEnrollDetailManager(EnrollDetailManager enrollDetailManager) {
		this.enrollDetailManager = enrollDetailManager;
	}

	@Autowired
	public void setAttachmentManager(AttachmentManager attachmentManager) {
		this.attachmentManager = attachmentManager;
	}

	@Autowired
	public void setNoticeManager(NoticeManager noticeManager) {
		this.noticeManager = noticeManager;
	}

	@Autowired
	public void setSpecialManager(SpecialManager specialManager) {
		this.specialManager = specialManager;
	}

}
